<?php

require_once dirname(__FILE__) . '/../lib/peticionesGeneratorConfiguration.class.php';
require_once dirname(__FILE__) . '/../lib/peticionesGeneratorHelper.class.php';

/**
 * peticiones actions.
 *
 * @package    SistemaWebUnlSenescyt
 * @subpackage peticiones
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class peticionesActions extends autoPeticionesActions {

//    public function executeUpdate(sfWebRequest $request) {
//        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
//        $this->forward404Unless($peticion = Doctrine_Core::getTable('peticion')->find(array($request->getParameter('id'))), sprintf('Object peticion does not exist (%s).', $request->getParameter('id')));
//        $this->form = new PeticionForm($peticion);
//        $this->processForm($request, $this->form);
//
//        $this->setTemplate('edit');
//        $newuser = new sfGuardUser();
//        $newuser->setDni($form->getObject()->getDni());
//        $newuser->setNombres($form->getObject()->setNombres());
//        $newuser->setApellidos($form->getObject()->getApellidos());
//        $newuser->setSexo($form->getObject()->getSexo());
//        $newuser->setEmailAddress($form->getObject()->setEmailAddress());
//        $newuser->setDireccion($form->getObject()->getDireccion());
//        $newuser->setTelefono($form->getObject()->getTelefono());
//        $newuser->setTelefonoMovil($form->getObject()->getTelefonoMovil());
//        $newuser->setUsername($form->getObject()->getUsername());
//        $newuser->setPassword($form->getObject()->getPassword());
//        $newuser->save();
//        $this->executeDelete($request);
//    }
    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->dispatcher->notify(new sfEvent($this, 'admin.delete_object', array('object' => $this->getRoute()->getObject())));

        if ($this->getRoute()->getObject()->delete()) {
            $this->getUser()->setFlash('notice', 'La peticion a sido eliminada exitosamente');
        }

        $this->redirect('@peticion');
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $peticion = $form->save();

            $newuser = new sfGuardUser();
            $newuser->setDni($form->getObject()->getDni());
            $newuser->setFirstName($form->getObject()->getFirstName());
            $newuser->setLastName($form->getObject()->getLastName());
            $newuser->setSexo($form->getObject()->getSexo());
            $newuser->setEmailAddress($form->getObject()->getEmailAddress());
            $newuser->setDireccion($form->getObject()->getDireccion());
            $newuser->setTelefono($form->getObject()->getTelefono());
            $newuser->setTelefonoMovil($form->getObject()->getTelefonoMovil());
            $newuser->setUsername($form->getObject()->getUsername());
            $newuser->setPassword($form->getObject()->getPassword());
            //$newuser->getIsSuperAdmin()

            try {

                $newuser->save();
                $peticion->delete();
                $this->redirect('usuarios/editagregar?id=' . $newuser->getId());
            } catch (Exception $e) {
//                $this->getUser()->setFlash('error', 'Ya existe un usuario con el mismo correo electronico, no se puede aprobar la solicitud, sugerimos eliminarla');
                $this->redirect('peticiones/edit?id=' . $peticion->getId());
                
            }
        }
    }

}
